package com.inibb.app.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

/**
 * 反射的Utils函数集合.
 * 
 * 提供侵犯隐私的直接读取filed的能劄1�7.
 */
public class BeanUtils {

	protected static Logger logger = LoggerFactory.getLogger(BeanUtils.class);

	private BeanUtils() {
	}

	/**
	 * Read All Properties,无视private/protected修饰笄1�7,不经过getter函数.
	 */
	public static Object getFieldValue(Object object, String fieldName)
			throws NoSuchFieldException {
		Field field = getDeclaredField(object, fieldName);
		if (!field.isAccessible()) {
			field.setAccessible(true);
		}

		Object result = null;
		try {
			result = field.get(object);
		} catch (IllegalAccessException e) {
			logger.error("不可能抛出的异常{}", e.getMessage());
		}
		return result;
	}

	/**
	 * Set All Properties,无视private/protected修饰笄1�7,不经过setter函数.
	 */
	public static void setFieldValue(Object object, String fieldName,
			Object value) throws NoSuchFieldException {
		Field field = getDeclaredField(object, fieldName);
		if (!field.isAccessible()) {
			field.setAccessible(true);
		}
		try {
			field.set(object, value);
		} catch (IllegalAccessException e) {
			logger.error("不可能抛出的异常:{}", e.getMessage());
		}
	}

	/**
	 * 循环向上转型,获取对象的DeclaredField.
	 */
	public static Field getDeclaredField(Object object, String fieldName)
			throws NoSuchFieldException {
		Assert.notNull(object);
		return getDeclaredField(object.getClass(), fieldName);
	}

	/**
	 * 循环向上转型,获取类的DeclaredField.
	 */
	@SuppressWarnings("unchecked")
	public static Field getDeclaredField(Class clazz, String fieldName)
			throws NoSuchFieldException {
		Assert.notNull(clazz);
		Assert.hasText(fieldName);
		for (Class superClass = clazz; superClass != Object.class; superClass = superClass
				.getSuperclass()) {
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
				// Field不在当前类定乄1�7,继续向上转型
			}
		}
		throw new NoSuchFieldException("No such field: " + clazz.getName()
				+ '.' + fieldName);
	}
	
	/**
	 * 判断两个Object的基本类型属性是否相等
	 */
	@SuppressWarnings("unchecked")
	public static boolean isAllBasicFieldsEqual(Object oldObject, Object newObject, Class c)
			throws NoSuchFieldException {
		Field[] fields = c.getDeclaredFields();
		
		if ((oldObject != null && newObject == null) ||
				(oldObject == null && newObject != null)) {
			return false;
		}
		else if (oldObject != null && newObject != null) {
			for (Field f : fields) {
				try {
					String str = f.getName();
					String fieldName = str.substring(0, 1).toUpperCase()
							.concat(str.substring(1, str.length()));
					Object oldObjTemp = new Object();
					Object newObjTemp = new Object();

					if (f.getType().equals(String.class) ||
							f.getType().equals(int.class) ||
							f.getType().equals(Long.class)) {
						Method getMethod = c.getMethod("get" + fieldName, null);
						oldObjTemp = getMethod.invoke(oldObject, null);
						newObjTemp = getMethod.invoke(newObject, null);
						if((oldObjTemp != null && oldObjTemp != null) &&
								!oldObjTemp.equals(newObjTemp)){
							return false;
						}
					}
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				} 
			}
		}
		else if(oldObject == null && newObject == null){
			return true;
		}
		
		return true;
	}
}
